package com.zj.config;

import com.zj.pojo.Dog;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

//spring4.3以前使用PropertySourcesPlaceholderConfigurer,需要自己手动配置到spring容器
//4.3以后使用DefaultPropertySourceFactory来解析配置资源加载绑定
//DefaultPropertySourceFactory可以处理xml、property配置文件
//通过设置factory=YamlPropertySourceFactory.class可以解析yaml配置文件
@PropertySource(value = "classpath:config.yml",encoding = "UTF-8",factory = YamlPropertySourceFactory.class)
public class JdbcConfig {
    @Value("${spring.jdbc.name}")
    private String name;
    public JdbcConfig(){
        new PropertySourcesPlaceholderConfigurer();
        new DefaultPropertySourceFactory();

        System.out.println("JdbcConfig create");
    }

    @Bean
    DataSource createDataSource(){
        System.out.println("createDataSource"+name);
        return new DriverManagerDataSource();
    }

    @Bean
//    @Lazy
    Dog createDog(){
        return new Dog();
    }
}

